package SoftwareEngineering01.WordFrequence.Step3;

import SoftwareEngineering01.WordFrequence.readFile.CompareWith;

import java.util.*;

public class Counting_02 {
    public static void counting_02(String string,int n){
        String[] letterlist = string.split("[^a-zA-Z0-9\\s]+");
        Map<String,Integer> map = new HashMap<String,Integer>();
        int sum = 0;
        for (int i = 0; i < letterlist.length; i++) {
            letterlist[i] = letterlist[i].trim();
            letterlist[i].replaceAll("\\n\\r"," ");
            String[] s = letterlist[i].split("\\s+");
            if(s.length != n){
                continue;
            }
            if(!map.containsKey(letterlist[i])){
                map.put(letterlist[i],1);
            }else{
                Integer num = map.get(letterlist[i]);
                map.put(letterlist[i],num+1);
            }
            sum++;
        }
        List<Map.Entry<String,Integer>> list = new ArrayList<>();
        list.addAll(map.entrySet());
        CompareWith compareWith = new CompareWith();
        Collections.sort(list,compareWith);
        for(Map.Entry<String,Integer> item:list){
            double per = item.getValue()*1.0 / sum *100;
            System.out.printf("%s: %.2f%%\n",item.getKey(),per);
        }
    }
}
